package com.approval.approvalapp.myself.activity;

import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.approval.approvalapp.R;
import com.approval.approvalapp.bean.WdhysBean;
import com.approval.approvalapp.util.DensityUtil;
import com.approval.approvalapp.util.SpUtil;
import com.approval.approvalapp.util.base.BaseActivity;
import com.approval.approvalapp.util.net.NetRequsetUtil;
import com.approval.approvalapp.util.net.interfacepac.NetDownResponse;
import com.approval.approvalapp.util.view.CustomTouchSwipeRefreshLayout;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.CenterPopupView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

//我的会议室申请
public class WdHysSqActivity extends BaseActivity {


    @Bind(R.id.back_iv)
    ImageView backIv;
    @Bind(R.id.titleTv1)
    TextView titleTv1;
    @Bind(R.id.state)
    TextView state;
    @Bind(R.id.state_ll)
    LinearLayout stateLl;
    @Bind(R.id.xrv)
    XRecyclerView xrv;
    @Bind(R.id.swipeRefreshLayout)
    CustomTouchSwipeRefreshLayout swipeRefreshLayout;
    @Bind(R.id.empty_ll)
    LinearLayout em;
    private List<WdhysBean> wdhysBeans = new ArrayList<>();
    private List<WdhysBean> wdhysBeansdb = new ArrayList<>();
    private List<WdhysBean> wdhysBeansyb = new ArrayList<>();
    private Adapter adapter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_wd_hys_sq;
    }

    @Override
    public void initView() {
        adapter = new Adapter();
        adapter.setWdhysBeans(wdhysBeans);
        xrv.setLayoutManager(new LinearLayoutManager(WdHysSqActivity.this));
        xrv.setAdapter(adapter);

        swipeRefreshLayout.setRefreshing(true);
        swipeRefreshLayout.postDelayed(new Runnable() {
            @Override
            public void run() {
                refreshData();
            }
        }, 500);

        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        xrv.setNoMore(false);
                        swipeRefreshLayout.setRefreshing(true);
                        refreshData();

                    }
                }, 500);
            }
        });

        xrv.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {

            }

            @Override
            public void onLoadMore() {
                swipeRefreshLayout.setRefreshing(false);
                xrv.setNoMore(true);
            }
        });
    }

    @Override
    public void initData() {

    }

    private void refreshData() {
        NetRequsetUtil.getInstance().netRequestGet("v1/center/my_meeting", new NetDownResponse() {
            @Override
            public void success(String str) {
                //初始化清空
                wdhysBeans.clear();
                wdhysBeansdb.clear();
                wdhysBeansyb.clear();

                wdhysBeans = JSON.parseArray(str, WdhysBean.class);

                //得到代办
                for (int i = 0; i < wdhysBeans.size(); i++) {
                    //代办
                    if (wdhysBeans.get(i).getIs_have().equals("0")) {
                        wdhysBeansdb.add(wdhysBeans.get(i));
                    } else {
                        //已办 1 2
                        wdhysBeansyb.add(wdhysBeans.get(i));
                    }


                    if (state.getText().toString().equals("全部")) {
                        adapter.setWdhysBeans(wdhysBeans);
                        if (wdhysBeans == null || wdhysBeans.size() == 0) {
                            em.setVisibility(View.VISIBLE);
                        } else {
                            em.setVisibility(View.GONE);
                        }
                    } else if (state.getText().toString().equals("待审批")) {
                        adapter.setWdhysBeans(wdhysBeansdb);
                        if (wdhysBeansdb == null || wdhysBeansdb.size() == 0) {
                            em.setVisibility(View.VISIBLE);
                        } else {
                            em.setVisibility(View.GONE);
                        }
                    } else {
                        adapter.setWdhysBeans(wdhysBeansyb);
                        if (wdhysBeansyb == null || wdhysBeansyb.size() == 0) {
                            em.setVisibility(View.VISIBLE);
                        } else {
                            em.setVisibility(View.GONE);
                        }
                    }
                }
            }

            @Override
            public void errowithresponse(String str) {

            }

            @Override
            public void erro() {

            }

            @Override
            public void finish() {
                if (swipeRefreshLayout != null){
                    swipeRefreshLayout.setRefreshing(false);
                }

            }
        });

    }

    @OnClick({R.id.back_iv, R.id.state_ll})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_iv:
                finish();
                break;
            case R.id.state_ll:
                new XPopup.Builder(this)
                        .autoOpenSoftInput(true)
                        .asCustom(new WdHysSqActivity.CustomPopup(this))
                        .show();
                break;
        }
    }


    public class CustomPopup extends CenterPopupView {
        public CustomPopup(@NonNull Context context) {
            super(context);
        }

        @Override
        protected int getImplLayoutId() {
            return R.layout.custom_popup;
        }

        @Override
        protected void onCreate() {
            super.onCreate();
            final ImageView iv1 = findViewById(R.id.iv1);
            final ImageView iv2 = findViewById(R.id.iv2);
            final ImageView iv3 = findViewById(R.id.iv3);

            if (state.getText().toString().equals("全部")) {
                iv1.setVisibility(VISIBLE);
                iv2.setVisibility(INVISIBLE);
                iv3.setVisibility(INVISIBLE);
            } else if (state.getText().toString().equals("待审批")) {
                iv1.setVisibility(INVISIBLE);
                iv2.setVisibility(VISIBLE);
                iv3.setVisibility(INVISIBLE);
            } else {
                iv1.setVisibility(INVISIBLE);
                iv2.setVisibility(INVISIBLE);
                iv3.setVisibility(VISIBLE);
            }


            findViewById(R.id.ll1).setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    state.setText("全部");
                    dismiss();

                    adapter.setWdhysBeans(wdhysBeans);

                    if (wdhysBeans == null || wdhysBeans.size() == 0) {
                        em.setVisibility(View.VISIBLE);
                    } else {
                        em.setVisibility(View.GONE);
                    }
                }
            });
            findViewById(R.id.ll2).setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    state.setText("待审批");
                    dismiss();

                    adapter.setWdhysBeans(wdhysBeansdb);

                    if (wdhysBeansdb == null || wdhysBeansdb.size() == 0) {
                        em.setVisibility(View.VISIBLE);
                    } else {
                        em.setVisibility(View.GONE);
                    }

                }
            });
            findViewById(R.id.ll3).setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    state.setText("已审批");
                    dismiss();

                    adapter.setWdhysBeans(wdhysBeansyb);

                    if (wdhysBeansyb == null || wdhysBeansyb.size() == 0) {
                        em.setVisibility(View.VISIBLE);
                    } else {
                        em.setVisibility(View.GONE);
                    }
                }
            });
        }

        @Override
        protected void onShow() {
            super.onShow();
        }

        @Override
        protected int getMaxWidth() {
            return DensityUtil.dip2px(WdHysSqActivity.this, 280f);
        }

    }


    class Adapter extends RecyclerView.Adapter<Adapter.Holder> {
        List<WdhysBean> wdhysBeans;

        public void setWdhysBeans(List<WdhysBean> wdhysBeans) {
            this.wdhysBeans = wdhysBeans;
            notifyDataSetChanged();
        }

        @Override
        public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_wdhys, parent, false);
            Holder holder = new Holder(view);
            return holder;
        }

        @Override
        public void onBindViewHolder(Holder holder, int position) {
            holder.tv1.setText(SpUtil.getString(WdHysSqActivity.this,SpUtil
            .USER_RUN_NICKNAME,"") + "的会议室申请-"+wdhysBeans.get(position).getImPlaceN());
//            SimpleDateFormat sdf = new SimpleDateFormat("yyyy MM-dd HH:mm");
//            Long timeStamp = new Long(wdhysBeans.get(position).getAdd_time()) * 1000;  //获取当前时间戳
//            String timese = sdf.format(new Date(Long.parseLong(String.valueOf(timeStamp))));
//            holder.tv2.setText(timese);
            holder.tv2.setVisibility(View.GONE);
            holder.tv3.setText("会议主题: " + wdhysBeans.get(position).getTitle());
            holder.tv4.setText("开始时间: " + wdhysBeans.get(position).getStart());
            holder.tv5.setText("结束时间: " + wdhysBeans.get(position).getEnd());
            if (wdhysBeans.get(position).getIs_have().equals("1") ){
                holder.tv6.setText("已通过");
                holder.tv6.setTextColor(Color.parseColor("#1465FD"));
            }else if (wdhysBeans.get(position).getIs_have().equals("2")){
                holder.tv6.setText("未通过");
                holder.tv6.setTextColor(Color.parseColor("#DE463A"));
            }else {
                holder.tv6.setText("待审核");
                holder.tv6.setTextColor(Color.parseColor("#F8AF35"));
            }
        }

        @Override
        public int getItemCount() {
            if (wdhysBeans == null) {
                return 0;
            } else {
                return wdhysBeans.size();
            }
        }

        class Holder extends RecyclerView.ViewHolder {
            @Bind(R.id.tv1)
            TextView tv1;
            @Bind(R.id.tv2)
            TextView tv2;
            @Bind(R.id.tv3)
            TextView tv3;
            @Bind(R.id.tv4)
            TextView tv4;
            @Bind(R.id.tv5)
            TextView tv5;
            @Bind(R.id.tv6)
            TextView tv6;
            public Holder(View itemView) {
                super(itemView);
                ButterKnife.bind(this, itemView);
            }
        }

    }

}
